package com.jia.together.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jia.together.entity.*;
import com.jia.together.entity.Course;
import com.jia.together.mapper.CourseMapper;
import com.jia.together.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jia.together.utils.JwtUtil;
import com.jia.together.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dujiajia
 * @since 2021-02-20
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private CourseKnowledgeService courseKnowledgeService;

    @Autowired
    private CourseTypeService courseTypeService;

    @Autowired
    private MemberCourseService memberCourseService;

    @Override
    public Map<String, Object> selectPageList(String typeId,Long pageNum, Long pageSize) {

        Page<Course> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(typeId)){
            wrapper.eq("course_type_id",typeId);
        }
        wrapper.orderByAsc("sort");

        wrapper.orderByDesc("gmt_create");
        Page<Course> resourcePage = baseMapper.selectPage(page, wrapper);
        List<Course> records = resourcePage.getRecords();
        long total = resourcePage.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("records", records);
        map.put("total", total);
        return map;

    }

    @Override
    public Map<String, Object> selectChapters(Long pageNum, Long pageSize) {
        Page<Course> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");

        Page<Course> resourcePage = baseMapper.selectPage(page, wrapper);
        List<Course> courseList = resourcePage.getRecords();
        courseList.forEach(item->{
            String courseId = item.getId();
            List<Chapter> chapterList = chapterService.selectByCourseId(courseId);
            item.setChapters(chapterList);
        });
        long total = resourcePage.getTotal();
        Map<String, Object> map = new HashMap<>();

        map.put("records", courseList);
        map.put("total", total);
        return map;
    }

    @Override
    public Course selectDetails(String id) {
        Course course = baseMapper.selectById(id);

        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",course.getId());
        wrapper.orderByAsc("sort");
        List<Chapter> chapterList = chapterService.list(wrapper);
        course.setChapters(chapterList);

        Teacher teacher = teacherService.getById(course.getTeacherId());
        course.setTeacherInfo(teacher);

        QueryWrapper<CourseKnowledge> knowWrapper = new QueryWrapper<>();
        knowWrapper.eq("course_id",course.getId());
        CourseKnowledge one = courseKnowledgeService.getOne(knowWrapper);
        course.setCourseKnowledge(one);
        return course;
    }

    @Override
    public Map<String, Object> selectByTypeId(String courseTypeId, Long pageNum, Long pageSize) {
        Page<Course> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("course_type_id",courseTypeId);
        wrapper.orderByAsc("sort");
        Page<Course> coursePage = baseMapper.selectPage(page, wrapper);
        List<Course> records = coursePage.getRecords();
        CourseType courseType = courseTypeService.getById(courseTypeId);
        String courseTypeTitle = courseType.getTitle();
        long total = coursePage.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("records",records);
        map.put("total",total);
        map.put("courseTypeTitle",courseTypeTitle);
        return map;
    }

    @Override
    public void click(String courseId, String memberId) {

        if(!StringUtils.isEmpty(memberId)){
            // 点击添加学习课程记录
            QueryWrapper<MemberCourse> wrapper = new QueryWrapper<>();
            wrapper.eq("study_course_id",courseId);
            wrapper.eq("member_id",memberId);
            MemberCourse one = memberCourseService.getOne(wrapper);
            if (one == null){
                MemberCourse memberCourse = new MemberCourse();
                memberCourse.setMemberId(memberId);
                memberCourse.setStudyCourseId(courseId);
                memberCourseService.save(memberCourse);
            }
        }

        // 点击添加查看逻辑
        Object redisCourseViews = redisUtil.get(courseId + "_course_views");
        if(redisCourseViews == null){
            Course course = baseMapper.selectById(courseId);
            Integer views = course.getViews();
            redisUtil.set(courseId + "_course_views",views+1);
        }else{
            redisUtil.set(courseId + "_course_views",(((Integer) redisCourseViews) +1));
        }
    }
    // 定时器 1小时执行一次
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void updateCourseViews() {
        redisUtil.del("courseTypes");
        List<Course> courses = baseMapper.selectList(null);
        courses.forEach(course -> {
            Integer redisCourseView = (Integer) redisUtil.get(course.getId() + "_course_views");
            course.setViews(redisCourseView);
            baseMapper.updateById(course);
        });
    }
}
